package mbp.gui.frames;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextField;
import javax.swing.JButton;

import mbp.base.Record;
import mbp.base.ResourceSet;
import mbp.core.Resource;
import mbp.message.resource;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class TermEditor extends JFrame implements ActionListener
{

	private JPanel contentPane;
	private JTextField txtTerm;
	private JButton btnSave;
	private Record<Resource, Object[]> resourceSet;
	private DefaultTableModel TModel;
	private int row;

	/**
	 * Create the frame.
	 */
	public TermEditor(Record<Resource, Object[]> resourceSet, DefaultTableModel TModel, int row) {
		this.resourceSet= resourceSet;
		this.TModel = TModel;
		this.row = row;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(200, 200, 220, 78);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		txtTerm = new JTextField();
		txtTerm.setBounds(10, 11, 90, 20);
		contentPane.add(txtTerm);
		txtTerm.setColumns(10);
		txtTerm.setText(resourceSet.value1[1].toString());
		
		btnSave = new JButton("Save");
		btnSave.setBounds(110, 10, 90, 23);
		contentPane.add(btnSave);
		btnSave.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnSave)
		{
			try
			{
				//this.resourceSet.term = new Integer(Integer.parseInt(txtTerm.getText()));
				this.resourceSet.value1[1] = new Integer(Integer.parseInt(txtTerm.getText()));
				TModel.setValueAt(this.resourceSet.value1[1].toString(), row, 2);
			}
			catch(Exception ex)
			{
				JOptionPane.showMessageDialog(this, ex.getMessage());
				return;
			}
			this.dispose();
		}
		// TODO Auto-generated method stub
		
	}
	
}
